#!/bin/bash

old_vol_id=$1

if [[ $old_vol_id == "" ]];
then
  echo "Usage: recover-volumes.sh <old_vol_id>"
  exit 1
fi

shell_pwd=`cd "$( dirname "$0" )" && pwd`
if [ ! -d "${shell_pwd}/cache" ];
then
  mkdir ${shell_pwd}/cache
fi
stat_file="${shell_pwd}/cache/old_${old_vol_id}.stat"
new_stat_file="${shell_pwd}/cache/new_for_${old_vol_id}.stat"
python_env="export PYTHONPATH=/pitrix/lib/pitrix-common:/pitrix/lib/pitrix-cli:\${PYTHONPATH};"
cli_conf="/pitrix/conf/client.yaml"
describe_cli="/pitrix/cli/describe-volumes"
create_cli="/pitrix/cli/create-volumes"
job_cli="/pitrix/cli/describe-jobs"
poweroff_s2_cli="/pitrix/cli/poweroff-s2-servers"
poweron_s2_cli="/pitrix/cli/poweron-s2-servers"
webservice_host=`grep webservice /etc/hosts|head -n 1|awk '{print $3}'`

echo "Getting old volume stats to cache/old_${old_vol_id}.stat..."
echo "=== === === === ==="
ssh -Tq $webservice_host "${python_env}${describe_cli} -v ${old_vol_id} -f ${cli_conf}" >${stat_file}
hyper_id=`grep '"host_machine":"' ${stat_file}|tail -n 1|awk -F'\"' '{print $4}'`
owner_id=`grep '"owner":"' ${stat_file}|tail -n 1|awk -F'\"' '{print $4}'`
size=`grep '"size":' ${stat_file}|tail -n 1|awk -F'\"|:|,' '{print $4}'`
zone_id=`grep '"zone_id":"' ${stat_file}|tail -n 1|awk -F'\"' '{print $4}'`
volume_type=`grep '"volume_type":' ${stat_file}|tail -n 1|grep -v "\[\]"|awk -F'\"|:|,' '{print $4}'`
volume_name=`grep '   "volume_name":"' ${stat_file}|tail -n 1|awk -F'\"' '{print $4}'`

if [[ $hyper_id == "" ]];
then
  echo "Error: ${old_vol_id} is not found!"
  exit 1
fi


echo "Creating new volume with old stats..."
echo "=== === === === ==="
if [[ ${old_vol_id} == "vos-"* ]]; then
  rg_name=$(ssh -Tq $hyper_id "/usr/bin/neonsan list_volume -detail|grep ${old_vol_id}"|awk -F'[| ]+' '{print $6}')
  create_cli="${python_env}${create_cli} -s ${size} -t ${volume_type} --rg=${rg_name} --volume_name=${volume_name} -H ${hyper_id} -u ${owner_id} -z ${zone_id} -f ${cli_conf}"
else
  create_cli="${python_env}${create_cli} -s ${size} -t ${volume_type} --volume_name=${volume_name} -H ${hyper_id} -u ${owner_id} -z ${zone_id} -f ${cli_conf}"
fi
ssh -Tq $webservice_host "${create_cli}" >${new_stat_file}
if [[ $? != 0 ]];
then
  echo "Error: create new volume failed!"
  exit 1
fi

job_id=`grep '"job_id":"' ${new_stat_file}|tail -n 1|awk -F'\"' '{print $4}'`
while true;
do
  echo "check job... ..."
  job_status=`ssh -Tq $webservice_host "${python_env}${job_cli} -j ${job_id} -f ${cli_conf}"|grep '"status":"successful",'|wc -l`
  if [[ ${job_status} == 1 ]];
  then
    echo "Create new volume job is successful..."
    echo "=== === === === ==="
    break
  fi
  sleep 1
done
new_vol_id=`grep -E '"vol-|"vos-' ${new_stat_file}|tail -n 1|awk -F'\"' '{print $2}'`


if [[ $volume_type == 2 ]];
then
  old_replic_vol=`echo ${old_vol_id}|sed 's/vol-/vox-/g'`
  new_replic_vol=`echo ${new_vol_id}|sed 's/vol-/vox-/g'`
  old_replic_hyper=`ssh -Tq $webservice_host "${python_env}${describe_cli} -v ${old_replic_vol} -f ${cli_conf}"|grep '"host_machine":"'|awk -F'\"' '{print $4}'`
  new_replic_hyper=`ssh -Tq $webservice_host "${python_env}${describe_cli} -v ${new_replic_vol} -f ${cli_conf}"|grep '"host_machine":"'|awk -F'\"' '{print $4}'`
  s2_server_id=`ssh -Tq $webservice_host "${python_env}${describe_cli} -v ${new_vol_id} -f ${cli_conf}"|grep '"export_id":"'|awk -F'\"' '{print $4}'`
  poweroff_log_file="${shell_pwd}/cache/poweroff_${s2_server_id}.stat"
  poweron_log_file="${shell_pwd}/cache/poweron_${s2_server_id}.stat"
  echo "Shut Down the S2 server..."
  echo "=== === === === ==="
  ssh -Tq $webservice_host "${python_env}${poweroff_s2_cli} -s ${s2_server_id} -f ${cli_conf}" >${poweroff_log_file}
  poweroff_job_id=`grep '"job_id":"' ${poweroff_log_file}|awk -F'\"' '{print $4}'`
  while true;
  do
    echo "check job... ..."
    poweroff_job_status=`ssh -Tq $webservice_host "${python_env}${job_cli} -j ${poweroff_job_id} -f ${cli_conf}"|grep '"status":"successful",'|wc -l`
    if [[ ${poweroff_job_status} == 1 ]];
    then
      echo "Shutting down S2 server is successful..."
      echo "=== === === === ==="
      break
    fi
    sleep 1
  done
  echo "Check old volume's data has been keeped..."
  echo "=== === === === ==="
  old_vol_file="/pitrix/data/container/__${old_replic_vol}/archive/${old_replic_vol}.img"
  ssh -Tq $old_replic_hyper "ls ${old_vol_file}"
  if [[ $? != 0 ]];
  then
    old_vol_file="/pitrix/data/container/archive/${old_replic_vol}.img"
    ssh -Tq $old_replic_hyper "ls ${old_vol_file}"
    if [[ $? != 0 ]];
    then
      echo "Error: old volume archive is non-existent, cannot recover!"
      exit 1
    fi
  fi
  echo "Start recover ${old_vol_id}..."
  echo "=== === === === ==="
  scp ${old_replic_hyper}:${old_vol_file} ${new_replic_hyper}:/pitrix/data/container/${new_replic_vol}.img
  echo "Start the S2 server..."
  echo "=== === === === ==="
  ssh -Tq $webservice_host "${python_env}${poweron_s2_cli} -s ${s2_server_id} -f ${cli_conf}" >${poweron_log_file}
  poweron_job_id=`grep '"job_id":"' ${poweron_log_file}|awk -F'\"' '{print $4}'`
  while true;
  do
    echo "check job... ..."
    poweron_job_status=`ssh -Tq $webservice_host "${python_env}${job_cli} -j ${poweron_job_id} -f ${cli_conf}"|grep '"status":"successful",'|wc -l`
    if [[ ${poweron_job_status} == 1 ]];
    then
      echo "Starting S2 server is successful..."
      echo "=== === === === ==="
      break
    fi
    sleep 1
  done
  echo "New volume ${new_vol_id} is recoverd by elder ${old_vol_id}..."
  echo "=== === === === ==="

elif [[ $volume_type == 0 ]] || [[ $volume_type == 3 ]] || [[ $volume_type == 100 ]] || [[ $volume_type == 200 ]];
then
  echo "Check old volume's data has been keeped..."
  echo "=== === === === ==="
  ssh -Tq $hyper_id "ls /pitrix/data/container/__${old_vol_id}/archive/${old_vol_id}.img"
  if [[ $? != 0 ]];
  then
    echo "Error: old volume archive is non-existent, cannot recover!"
    exit 1
  fi
  echo "Start recover ${old_vol_id}..."
  echo "=== === === === ==="
  ssh -Tq $hyper_id "cp -f /pitrix/data/container/__${old_vol_id}/archive/${old_vol_id}.img /pitrix/data/container/${new_vol_id}/${new_vol_id}.img"
  echo "New volume ${new_vol_id} is recoverd by elder ${old_vol_id}..."
  echo "=== === === === ==="

elif [[ $volume_type == 5 ]] || [[ $volume_type == 6 ]] || [[ $volume_type == 7 ]];
then
  echo "Check old volume's data has been keeped..."
  echo "=== === === === ==="
  ssh -Tq $hyper_id "/usr/bin/neonsan list_volume -detail|grep ${old_vol_id}"
  if [[ $? != 0 ]];
  then
    echo "Error: old volume archive is non-existent, cannot recover!"
    exit 1
  fi
  archive_name=`ssh -Tq $hyper_id "/usr/bin/neonsan list_volume -detail|grep ${old_vol_id}"|awk -F'[| ]+' '{print $3}'`
  echo "Start recover ${old_vol_id}..."
  echo "=== === === === ==="
  san_job_id=`ssh -Tq $hyper_id "/usr/bin/neonsan clone_volume -source_volume vol/${archive_name} -target_volume vol/${new_vol_id}.img|grep 'clone_volume_sync'"|awk -F'[| ]+' '{print $3}'`
  while true;
  do
    echo "check neonsan clone_volume job... ..."
    san_job_status=`ssh -Tq $hyper_id "/usr/bin/neonsan list_job"|grep ${san_job_id}|grep 'succeeded'|wc -l`
    if [[ ${san_job_status} == 1 ]];
    then
      echo "neonsan clone volume to ${new_vol_id} is successful..."
      echo "=== === === === ==="
      break
    fi
    sleep 5
  done
  echo "neonsan detach_clone..."
  echo "=== === === === ==="
  ssh -Tq $hyper_id "/usr/bin/neonsan detach_clone --source_volume vol/${archive_name} --target_volume vol/${new_vol_id}.img"
  echo "New volume ${new_vol_id} is recoverd by elder ${archive_name}..."
  echo "=== === === === ==="

else
  echo "Volume Type ${volume_type} is not support yet!"

fi
